<?php
/**
 * <b>RUBIK IT Solution & Development ltd.</b>
 *
 * PROJECT		: Rubik Ultimate Framework
 * @version 	: 1.1
 * COPYRIGHT    : 2008
 * ------------------------------------------------------
 *  
 * @author			hoadn <hoadn@rubik.com.vn>
 *
 * Created on	: Nov 14, 2008
 */

// Please scroll down to the end of file for changing content

/**
 * The class for keeping authenticate object of Rubik Ultimate project
 *
 */
RClassesImporter::cls('controllers.AbstractBackEndController');
RCoreImporter::cls( "mvc.AbstractController" );
class AuthenticateObject {
	var $user_id; 
	var $username;
	var $user_type;
}

////////////////////////////////////////////////////
define(SESS_AUTHEN_NAME, '_RU_AuthenticateObj'.SYS_WORK_ZONE);
session_start();

/**
 * The authenticate class of System
 *
 */
class SysAuthenticate extends AbstractController{
	

	/**
	 * Get the AuthenticateObject if the user logged in
	 *
	 * @return AuthenticateObject
	 */
	static function getAuthenticateObject() {
		return $_SESSION [SESS_AUTHEN_NAME];
	
	}
	
	/**
	 * Check if user is authenticated or not?
	 * NOTE the content of this static function can be change for each specific site
	 *
	 * @return boolean
	 */
	static function isLogged() {
		$authObj = SysAuthenticate::getAuthenticateObject();
		return ($authObj) ? true : false;
	}
	
	/**
	 * Check user for loggin back-end of the system. 
	 * If valid user --> set AuthenticateObject into SESSION and return the status
	 *
	 * @param string $username
	 * @param string $password
	 */
	static function backLogin($username, $password) {
		$obj = &BackAuthenticateFinder::getAuthenticate($username, $password);
		if ($obj) {
			$_SESSION[SESS_AUTHEN_NAME] = $obj;
			return true;
		}
		return false;
	}
	
	/**
	 * Check user for loggin front-end of the system. 
	 * If valid user --> set AuthenticateObject into SESSION and return the status
	 *
	 * @param string $username
	 * @param string $password
	 */
	function frontLogin($username, $password) {
		$obj = &FrontAuthenticateFinder::getAuthenticate($username, $password);
		if ($obj) {
			$app = $_REQUEST['_rurq'];
			/*$abc = explode('/', $app);
			
			if ($abc['1'] == 'event'){
				RubikHelper::debugPrePrint(SYS_ROOT_URL.'event/joinIt');
				$this->setRedirect("dsdfsdf") ;
				die("nam");
				$this->setRedirect(SYS_ROOT_URL.'event/joinIt') ;
			}
			*/
			$_SESSION[SESS_AUTHEN_NAME] = $obj;
			
			return true;
		}
		return false;
	}
	
	/**
	 * Logout
	 *
	 */
	static function logout(){
		unset($_SESSION[SESS_AUTHEN_NAME]);
	}
	
	/**
	 * Get current user_id
	 *
	 * @return int
	 */
	static function getUserId() {
		$obj = &SysAuthenticate::getAuthenticateObject();
		if ($obj) {
			return $obj->user_id;
		}
		return null;
	}
	
	/**
	 * Get current username
	 *
	 * @return string
	 */
	static function getUserName() {
		$obj = &SysAuthenticate::getAuthenticateObject();
		if ($obj) {
			return $obj->username;
		}
		return null;
	}
	
	/**
	 * Get current username
	 *
	 * @return string
	 */
	static function getUserType() {
		$obj = &SysAuthenticate::getAuthenticateObject();
		if ($obj) {
			return $obj->user_type;
		}
		return null;
	}
}

/**
 * WORKING ZONE here
 */

// init access right
require ( "initAccessRight.php" );

///////////////////////////////////////////////////////////////////////
// BEGIN authorization
if (SystemSite::isBackEnd()) {
	RPluginImporter::cls('authenticate.BackAuthenticateFinder');

	if (! SysAuthenticate::isLogged()) {
		$sysRes = SysRequest::getResource ();
		$loginRes = new SiteResource ( "authenticate", "login" );
		if (! $sysRes->checkCover ( $loginRes )) {
			$loginUrl = $loginRes->getLink ();
			RubikHelper::redirectToUrl($loginUrl);
		}
	}
}
UserAccessFactory::setSysUserGroup(SysAuthenticate::getUserType());
// END authorization
///////////////////////////////////////////////////////////////////////
?>